<template>
  <div>
    <table>
      <tr>
        <td>用户名：</td>
        <td><input type="text" v-model="username" /></td>
      </tr>
      <tr>
        <td>密码：</td>
        <td><input type="password" v-model="password" /></td>
      </tr>
      <tr>
        <td>手机号：</td>
        <td><input type="text" v-model="phone" /></td>
      </tr>
      <tr>
        <td><button @click="admin_reg">注册</button></td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      username: "",
      password: "",
      phone: ""
    };
  },
  methods: {
    //   注册
    admin_reg: function() {
      if (this.username) {
        if (this.password) {
          if (this.phone) {
            var phoneReg = /(^1\d{10}$)|(^[0-9]\d{7}$)/;

            if (!phoneReg.test(this.phone)) {
              alert("手机号码格式错误!");
              return false;
            } else {
              this.axios({
                url: "http://localhost:8000/admin_reg/",
                method: "POST",
                data: {
                  username: this.username,
                  password: this.password,
                  phone: this.phone,
                  is_admin: 1
                }
              })
                .then(res => {
                  if (res.status == 201) {
                    alert("注册成功");
                  } else {
                    alert("用户名已存在");
                  }
                })
                .catch(error => {
                  console.log(error);
                });
            }
          } else {
            alert("请输入手机号");
          }
        } else {
          alert("请输入密码");
        }
      } else {
        alert("请输入用户名");
      }
    }
  }
};
</script>

<style>
</style>